import Vue from 'vue'
import Vuex from 'vuex'


import axios from 'axios'
import createPersistedState from 'vuex-persistedstate'
Vue.use(Vuex)

export default new Vuex.Store({
  // strict:true,
  // 2. 配置为 vuex 的插件
  plugins: [createPersistedState()],
  state: {
    token: '',
    // token: localStorage.getItem('cmsVueToken') || ''
    userInfo: {}
  },
  getters: {
  },
  mutations: {
    //  更新 token 的 mutation 函数
    updateToken(state, newToken) {
      state.token = newToken
    },
    updataUserInfo(state, userinfo) {
      state.userInfo = userinfo
    }
  },
  actions: {
    async  updataUserInfo(context) {
      const { data: res } = await axios.get('/my/userinfo')
      // console.log(res);
      if (res.code == 0) {
        context.commit('updataUserInfo', res.data)
      }
    },
  },
  modules: {
  }
})
